package com.itheima.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.mapper.UmsResourceCategoryMapper;
import com.itheima.pojo.UmsResourceCategory;
import com.itheima.vo.PageResultVo;
import com.itheima.vo.Result;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;


@DubboService
public class UmsResourceCategoryImpl implements UmsResourceCategoryApi {

    @Autowired
    private UmsResourceCategoryMapper umsResourceCategoryMapper;

    @Override
    public boolean add(UmsResourceCategory umsResourceCategory) {
        boolean flag = false;

        List<UmsResourceCategory> list = findAll();
        for (UmsResourceCategory role : list) {
            if (StringUtils.pathEquals(role.getName(), umsResourceCategory.getName())) {
                flag = true;
                return flag;
            }
        }
        LocalDateTime localDateTime = LocalDateTime.now();
        umsResourceCategory.setCreateTime(localDateTime);
        umsResourceCategoryMapper.insert(umsResourceCategory);

        return flag;
    }

    @Override
    public void deleteByIds(Integer id) {
        umsResourceCategoryMapper.deleteById(id);
    }

    @Override
    public void update(Integer id,UmsResourceCategory umsResourceCategory) {
        LambdaUpdateWrapper<UmsResourceCategory> wrapper=new LambdaUpdateWrapper<>();
        wrapper.eq(UmsResourceCategory::getId,id);
        umsResourceCategoryMapper.update(umsResourceCategory,wrapper);
    }

    @Override
    public List<UmsResourceCategory> findAll() {
        List<UmsResourceCategory> list = umsResourceCategoryMapper.selectList(new LambdaQueryWrapper<UmsResourceCategory>().ne(UmsResourceCategory::getId,0));
        return list;
    }
}
